<template>
	<view class="diandan">
		<view class="diandan_index">
			<view class="top_add" @click="toadds()">

				<view style="display: flex;justify-content: center;flex-direction: column;">
					<text class="iconfont" style="color: red; font-size: 1.8rem;">&#xe60e;</text>
				</view>
				<view style="display: flex;justify-content: center;flex-direction: column; width: 70%">
					<view style="display: flex;justify-content: space-between;">
						<view style="width: 230px;">
							<view style="margin-bottom: 5px;">
								<text style="margin-right: 10px;">{{addlist.name}}</text><text>{{addlist.phone}}</text>
							</view>
							<view style="color: #2A2A2A; font-size: 14px;">
								<text>{{addlist.city}}</text>
							</view>
						</view>
						<view style="display: flex;justify-content: center;flex-direction: column;">
							<text class="iconfont" style="color: #8a8a8d;">&#xe63f;</text>
						</view>
					</view>
				</view>

			</view>
			<view class="body_index">
				<view style="width: 100%; padding-bottom: 10px; max-height: 395px; overflow-y: scroll;">
					<view v-for="item in listdata">
						<view style="margin: 10px;">
							<text class="iconfont"
								style="margin-right: 10px;">&#xe7a8;</text><text>{{item.storeName}}</text>
						</view>
						<view @click="toxiangQ(item)"
							style="display: flex;justify-content: space-between; width: 98%; margin-top: 15px;"
							v-for="item in item.goodsList">
							<view style="width: 100px;height: 100px; margin-left: 10px;">
								<image style="width: 100%;height: 100%; border-radius: 5px;" :src="item.cover" mode="">
								</image>
							</view>
							<view style="display:flex;justify-content: space-around;flex-direction: column;width: 65%;">
								<view class="">
									<view style="font-size: 14px;">
										{{item.name}}
									</view>
									<!-- 	<text style="font-size: 13px; color: #303133;">奶茶S6杯</text> -->
								</view>

								<view style="display: flex;justify-content: space-between;">
									<view class="">
										<text style="color: red;">￥</text>
										<text style="font-size: 1.2rem; color: red;">{{item.price_selling}}</text>
									</view>
									<view style="display: flex;justify-content: center; flex-direction: column;">
										<text style="color: #303133; font-size: 13px;">x{{item.cart_number}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>


			<view>
				<view style="background-color: #FFFFFF;border-radius: 5px;margin-top: 10px;">
					<view style="display: flex;justify-content: space-between;padding: 10px;">
						<view style="color: #323233; font-size: 14px;">
							商品总额
						</view>
						<view class="">
							<text style="font-size: 10px;">￥</text><text style="font-size: 14px;">{{qian}}</text>
						</view>
					</view>
					<view style="display: flex;justify-content: space-between;padding: 10px;">
						<view style="color: #323233; font-size: 14px;">
							运费
						</view>
						<view style="font-size: 14px;">
							<text>包邮</text>
						</view>
					</view>


				</view>
			</view>

		</view>





		<view class="foot_index">
			<view style="display: flex;justify-content: center;flex-direction: column;height: 100%;">
				<view class="foot_index_body">
					<view class="">
						<view>
							<text>合计：</text>
							<text style="color: red;">￥</text>
							<text style="color: #ff0000; font-size: 1.2rem; font-weight: 600;">{{qian}}</text>
						</view>
						<view style="font-size: 12px;">
							<!-- <text style="margin-right: 10px;">以优惠:110.00</text> -->
							<text>以实际支付金额为准</text>
						</view>
					</view>
					<view class="">
						<view
							style="width: 115px; height: 100%; border-radius: 50px; background-color: red; color: #FFFFFF; text-align: center; display: flex;justify-content: center;flex-direction: column;">
							<text @click="tijiao()">
								提交订单
							</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addlist: {},
				listdata: [],
				cart_ids: {
					cart_id:[]
				},
				qian: 0,
				yue: 0,
				sunqian: 0,
				yuefukuan: 0,
				dindanshuju: {
					listdataA: {

						data: {
							reduce: '',
							total: 0,
							productList: []
						}
					},

				},
				datast: {
					address: '',
					submitType: 'cart',
					items: [

					]
				}

			}
		},
		onShow() {
			this.cart_ids.cart_id = []
			this.addlist = {}

			var this_ = this
			uni.getStorage({
				key: 'shopddan',
				success: function(res) {
					
					for(var a =0;a<res.data.cart_id.length;a++){
						
						this_.cart_ids.cart_id.push(res.data.cart_id[a])
					}
					this_.$http.getgetyorder(this_.cart_ids).then(res => {
						
						this_.listdata = res.data
					})
					
				}
			})
			

			uni.getStorage({
				key: 'qiansum',
				success: function(res) {
					this_.qian = res.data
				}
			})


			uni.getStorage({
				key: 'xuanadds',
				success: function(res) {
					this_.addlist = res.data
				}
			})
			// this.logaddlist()
			// this.$http.getinfo().then(res => {

			// 	if (res.code == 40002 && this_.tijian == 0) {
			// 		this.shouquan = true
			// 	} else {
			// 		this.yue = res.data[0].lm_money
			// 		var reduceA = parseFloat(this.yue) - parseFloat(this.qian)
			// 		var reduceB = parseFloat(this.yue) - reduceA
			// 		this.yuefukuan = (reduceB).toFixed(2)
			// 		if (this.yuefukuan > this.yue) {
			// 			this.yuefukuan = this.yue
			// 			this.yuefukuan = (this.yuefukuan).toFixed(2)
			// 		}
			// 	}
			// 	this.sunqian = (this.qian - this.yue).toFixed(2)
			// 	if (this.sunqian < 0) {
			// 		this.sunqian = 0
			// 	}

			// })

		},
		methods: {
			toxiangQ(items) {

				uni.setStorage({
					key: 'xiangqing',
					data: items,
					success: function() {}
				});


				uni.navigateTo({
					url: '../xiangqing/xiangqing?item=' + items.id,
					fail: (res) => {
						console.log(res) //打印错误信息
					}
				});
			},
			logaddlist() {
				this.addlist = {}
				var this_ = this
				this.$http.getaddress().then(res => {
					this.addlist = {}
					for (var i = 0; i < res.data.length; i++) {
						if (res.data[i].top == 1) {
							this.addlist = {}
							this_.addlist = res.data[i]
						}
					}
				})
			},
			toadds() {
				uni.navigateTo({
					url: '../addsd/addsd'
				})
			},



			tijiao() {
				this.datast.items = []
				console.log(this.listdata);
				this.datast.address = this.addlist.code
				if (this.addlist.city == null) {
					uni.$u.toast('请选择地址');
				} else {
					var str = ''
					var shangpin = []
					var dianpu = []
					for (var a = 0; a < this.listdata.length; a++) {
						var shangpin = []
						for (var i = 0; i < this.listdata[a].goodsList.length; i++) {
							var obj = {}
							var objc = ''
							obj.code = this.listdata[a].goodsList[i].goods_code
							obj.goods_sku = this.listdata[a].goodsList[i].goods_sku
							obj.num = this.listdata[a].goodsList[i].cart_number
							objc = `${obj.code}@${obj.goods_sku}@${obj.num}`
							shangpin.push(objc)
						}
						var ssd = ''
						for (var c = 0; c < shangpin.length; c++) {
							if (c > 0) {
								ssd = ssd + "||" + shangpin[1 * c]
							} else {
								ssd = ssd + shangpin[c]
							}
						}
						var s = ssd
						dianpu.push(s)
					}
					console.log(dianpu);
					this.datast.items = dianpu
					// [G7545659919812191200@S7545660537694@1||G7545655434714838461@S7545657195397@10,G7544832862266839720@S7544833564569@1]
					// this.datast.items = JSON.stringify(this.datast.items)
					// var subStr=new RegExp(',');//创建正则表达式对象,不区分大小写,全局查找
					// this.datast.items=this.datast.items.replace(subStr,"||");//把'is'替换为空字符串


					this.datast.items = JSON.stringify(this.datast.items)
					console.log(this.datast);

					this.$http.postaddorder(this.datast).then(res => {
						uni.$u.toast(res.info);
						if (res.info == '库存不足') {
							uni.$u.toast(res.info);
						} else {

							if (res.code == 1) {

								if (this.addlist.id == null) {
									uni.$u.toast('请选择地址');
								} else {
									wx.requestPayment({ //调起支付
										"timeStamp": res.data.pay.timeStamp,
										"nonceStr": res.data.pay.nonceStr,
										"package": res.data.pay.package,
										"signType": res.data.pay.signType,
										"paySign": res.data.pay.paySign,
										'success': function(res) { // 接口调用成功的回调函数
											uni.$u.toast("支付成功");
											//TODO  跳转订单
											wx.reLaunch({
												url: '../orderdetails/orderdetails',
											})
										},
										'fail': function(res) { // 接口调用失败的回调函数
											uni.$u.toast("支付失败");
										}
									})
								}




								// setTimeout(() => {
								// 	uni.reLaunch({
								// 		url: '../orderdetails/orderdetails'
								// 	});
								// }, 800);
							} else {
								uni.$u.toast(res.msg);
							}
						}

					})



				}



			},

			onadduer() {



			}
		}
	}
</script>

<style>
	@font-face {
		font-family: 'iconfont';
		/* Project id 3367375 */
		src: url('//at.alicdn.com/t/font_3367375_x5c1krqbu2c.woff2?t=1652160518160') format('woff2'),
			url('//at.alicdn.com/t/font_3367375_x5c1krqbu2c.woff?t=1652160518160') format('woff'),
			url('//at.alicdn.com/t/font_3367375_x5c1krqbu2c.ttf?t=1652160518160') format('truetype');
	}

	.iconfont {
		font-family: "iconfont" !important;
		font-size: 16px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-webkit-text-stroke-width: 0.2px;
		-moz-osx-font-smoothing: grayscale;
	}

	.diandan {
		position: fixed;
		width: 100%;
		height: 100%;
		background-color: #f6f9f2;

	}

	.foot_index {
		position: absolute;
		bottom: 0;
		width: 100%;
		height: 55px;
		background-color: #FFFFFF;
	}

	.foot_index_body {
		width: 100%;
		display: flex;
		justify-content: space-around;
	}


	.diandan_index {
		margin: 10px;

	}

	.top_add {
		background-color: #FFFFFF;
		display: flex;
		justify-content: space-around;
		height: 60px;
		border-radius: 5px;
	}

	.body_index {
		margin-top: 10px;
		background-color: #FFFFFF;
		display: flex;
		justify-content: space-around;
		border-radius: 5px;
	}
</style>
